package com.geek.factory.exception;

import com.geek.factory.constant.SuccessConstant;
import com.geek.factory.result.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author chenmin
 * @desc TODO
 * @date 2025/6/20
 *
 * 哪些处理异常的方式?
 * 1,实现 HandlerExceptionResolver接口，ModelAndView resolveException()  适合前后端不分离
 * 2,@RestControllerAdvice +  @ExceptionHandler(HealthException.class) 适合前后端分离
 *
 * 全局异常处理器
 * 注解@RestControllerAdvice
 *      复合注解 @ControllerAdvice + @ResponseBody
 * 注解@ExceptionHandler(XxxException.class)
 */
@RestControllerAdvice
public class CustomExceptionHandler {

    @ExceptionHandler(AccessTokenInvalidException.class)
    public Result handler1(AccessTokenInvalidException ex){
        //10001
        return new Result(SuccessConstant.ACCESS_TOKEN_INVALID , ex.getMessage());
    }

    @ExceptionHandler(RefreshTokenInvalidException.class)
    public Result handler2(RefreshTokenInvalidException ex){
        //10002
        return new Result(SuccessConstant.REFRESH_TOKEN_INVALID , ex.getMessage());
    }

    @ExceptionHandler(FactoryException.class)
    public Result handler3(FactoryException ex){
        //9001
        return new Result(SuccessConstant.FAIL , ex.getMessage());
    }

    /*@ExceptionHandler(Exception.class)
    public Result handler2(Exception ex){
        return new Result(CodeConstant.FAIL , "未知错误");
    }*/

}
